package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("list")
    private R<List<ShoppingCart>> shoppingCart(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }
    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(StringUtils.checkValNotNull(shoppingCart.getDishId()),ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(StringUtils.checkValNotNull(shoppingCart.getSetmealId()),ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        qw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one==null){
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else {
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }
        return R.success("添加成功");
    }
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(StringUtils.checkValNotNull(shoppingCart.getDishId()),ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(StringUtils.checkValNotNull(shoppingCart.getSetmealId()),ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        qw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one.getNumber()==1){
            shoppingCartService.remove(qw);
            return R.success("减少成功");
        }else{
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
            return R.success("移除成功");
        }
    }
    @DeleteMapping("clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        qw.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(qw);
        return R.success("清空成功");
    }

}
